"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const enums_1 = require("../utils/enums");
function default_1(app) {
    const { socket } = app.locals;
    const { userId, classId } = socket.handshake.query;
    socket.on('disconnect', function () {
        const { rooms, members } = app.locals;
        const { classType } = rooms[classId];
        const member = members[userId];
        if (member) {
            const options = {
                ...members[userId].json(),
                classId
            };
            if (classType === enums_1.RoomTypeEnums.TEMPORARY) {
                socket.to(classId).emit('dissolve', options);
                if (rooms[classId] && rooms[classId].stop) {
                    app.locals.rooms[classId].stop();
                }
                socket.leave(classId);
            }
            if (classType === enums_1.RoomTypeEnums.RESIDENT) {
                socket.to(classId).emit('offline', options);
            }
        }
        if (members[userId]) {
            delete app.locals.members[userId];
        }
        if (rooms[classId]) {
            app.locals.rooms[classId].removeMember(userId);
        }
    });
    socket.on('disconnecting', function () {
        console.log('--------disconnecting-------');
    });
    socket.on('error', function () {
        console.log('--------error-------');
    });
}
exports.default = default_1;
